package com.liang.kborcl.controller;


import com.liang.kborcl.domain.UserDO;
import com.liang.kborcl.repository.UserRepository;
import org.apache.shardingsphere.core.route.router.masterslave.MasterVisitedManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Optional;


@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    UserRepository dao;


    /**
     * 只查：slave
     *
     * @return {@link UserDO}
     */
    @RequestMapping("/read")
    public List<UserDO> get(){
        UserDO userDO = new UserDO();
        userDO.setName("liang");
        Example<UserDO> of = Example.of(userDO);
        List<UserDO> all = dao.findAll(of);
        return all;
    }


    /**
     * 只写：master
     *
     * @return {@link UserDO}
     */
    @RequestMapping("/write")
    public UserDO write() {
        // 1.保存
        UserDO userDO = new UserDO();
        userDO.setName("liang");
        userDO.setAge(20);
        dao.save(userDO);
        return userDO;
    }


    /**
     * 先写后查：master
     *
     * @return {@link List}<{@link UserDO}>
     */
    @RequestMapping("/wr")
    public List<UserDO> wr(){
        // 1.保存
        UserDO userDO = new UserDO();
        userDO.setName("liang");
        userDO.setAge(20);
        dao.save(userDO);

        // 2.查询
        UserDO query = new UserDO();
        query.setName("liang");
        Example<UserDO> of = Example.of(query);
        List<UserDO> all = dao.findAll(of);
        return all;
    }


    /**
     * 先查后写后查：slave、master、master
     *
     * @return {@link List}<{@link UserDO}>
     */
    @RequestMapping("/rwr")
    public List<UserDO> rwr(){
        // 1.查询
        UserDO query = new UserDO();
        query.setName("liang");
        Example<UserDO> of = Example.of(query);
        List<UserDO> all = dao.findAll(of);

        // 2.保存
        UserDO userDO = new UserDO();
        userDO.setName("liang");
        userDO.setAge(20);
        dao.save(userDO);

        // 3.查询
        UserDO query2 = new UserDO();
        query.setName("liang");
        Example<UserDO> of2 = Example.of(query2);
        List<UserDO> all2 = dao.findAll(of2);
        return all;
    }


    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping("/jdbcR")
    public List jdbcR(){
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from t_user");
        return  maps;
    }

    @RequestMapping("/jdbcW")
    public Integer jdbcW(){
        String sql = "update t_user set age = 111 where name = 'liang'";
        int update = jdbcTemplate.update(sql);
        return  update;
    }


    /**
     * jdbc 读写读：slave、master、slave
     *
     * @return {@link List}
     */
    @RequestMapping("/jdbcRWR")
    public List jdbcRWR(){
        MasterVisitedManager.setMasterVisited();
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from t_user");

        String sql = "update t_user set age = 111 where name = 'liang'";
        int update = jdbcTemplate.update(sql);

        List<Map<String, Object>> maps2 = jdbcTemplate.queryForList("select * from t_user");
        return  maps2;
    }



}
